package com.doubledr.light_lock.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.doubledr.light_lock.R;

import java.util.List;
import java.util.Map;

public class FileListAdapter extends BaseAdapter {
    private Context context = null;
    private List<Map<String, Object>> datalist;

    public FileListAdapter(Context context, List<Map<String, Object>> datalist){
        this.context = context;
        this.datalist = datalist;
    }
    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FileListAdapter.ViewHolder mHolder;
        if (convertView == null) {
            mHolder = new FileListAdapter.ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.file_item, null, true);
            mHolder.file_img = convertView.findViewById(R.id.fileImg);
            mHolder.file_name =  convertView.findViewById(R.id.fileName);
            mHolder.file_time =  convertView.findViewById(R.id.fileTime);
            convertView.setTag(mHolder);
        } else {
            mHolder = (FileListAdapter.ViewHolder) convertView.getTag();
        }
        String title = datalist.get(position).get("name").toString();
        String img = datalist.get(position).get("img").toString();
        String time = datalist.get(position).get("time").toString();

        int picId = Integer.parseInt(img);
        mHolder.file_img.setImageResource(picId);
        mHolder.file_name.setText(title);
        mHolder.file_time.setText(time);
        return convertView;
    }
    class ViewHolder {
        private ImageView file_img;
        private TextView file_name;
        private TextView file_time;
    }
}
